package com.manage.controller;

import com.manage.pojo.CommentReplys;
import com.manage.service.CommentReplysService;
import com.manage.utils.FBJSONResult;
import com.manage.utils.PagedResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Api(value = "有关评论回复信息的操作", tags = {"有关评论回复的操作"})
public class CommentReplysController {

    @Autowired
    private CommentReplysService commentReplysService;

    @ApiOperation(value = "评论回复信息添加", notes = "评论回复信息添加接口")
    @PostMapping("/commentReply/commentReplyRegist")
    public FBJSONResult commentReplyRegist(@RequestBody CommentReplys commentReply) throws Exception {
        commentReply.setShoesId(commentReply.getShoesId());
        commentReply.setCommentReplyContent(commentReply.getCommentReplyContent());
        commentReply.setCommentReplyImage(commentReply.getCommentReplyImage());
        commentReply.setUserUsername(commentReply.getUserUsername());
        commentReply.setUserAvator(commentReply.getUserAvator());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        commentReply.setCommentReplyTime(sdf.format(new Date()));
        commentReplysService.saveCommentReplys(commentReply);
        return FBJSONResult.ok(commentReply);
    }

    @ApiOperation(value = "评论回复信息删除", notes = "评论回复信息删除接口")
    @ApiImplicitParam(name = "commentReplyId", value = "评论回复id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/commentReply/commentReplyDelete")
    public FBJSONResult commentReplyDelete(String commentReplyId) throws Exception {
        if(StringUtils.isBlank(commentReplyId)) {
            return FBJSONResult.errorMsg("评论回复id不能为空");
        }
        boolean commentReplyIdIsExit = commentReplysService.queryCommentReplysIsExit(commentReplyId);
        if(commentReplyIdIsExit) {
            commentReplysService.commentReplyDelete(commentReplyId);
        }
        return FBJSONResult.ok("删除成功");
    }

    @ApiOperation(value = "评论回复信息查询(根据评论回复id)", notes = "评论回复信息查询接口(根据评论回复id)")
    @ApiImplicitParam(name = "commentReplyId", value = "评论回复id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/commentReply/queryCommentReplyInfoByCommentReplyId")
    public FBJSONResult queryCommentReplyInfoByCommentReplyId(String commentReplyId) throws Exception {
        if(StringUtils.isBlank(commentReplyId)) {
            return FBJSONResult.errorMsg("评论回复id不能为空");
        }
        CommentReplys commtentReplyResult = commentReplysService.queryCommentReplysInfoByCommentReplyId(commentReplyId);
        return FBJSONResult.ok(commtentReplyResult);
    }

    @ApiOperation(value = "评论回复信息列表查询(根据鞋id)", notes = "评论回复信息列表查询接口(根据鞋id)")
    @ApiImplicitParam(name = "shoesId", value = "鞋id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/commentReply/queryCommentReplysInfoList")
    public FBJSONResult queryCommentReplysInfoList(Integer page, String shoesId) throws Exception {
        if(StringUtils.isBlank(shoesId)) {
            return FBJSONResult.errorMsg("鞋id不能为空");
        }
        if(page == null) {
            page = 1;
        }
        PagedResult result = commentReplysService.queryCommentReplysInfoByShoesId(page, 10, shoesId);
        return FBJSONResult.ok(result);
    }

}
